<?php
class UnionMemberDetailsController extends AppController{
	var $name = 'UnionMemberDetails';
	var $uses = array('UnionMember', 'UnionMemberDetail', 'Religion', 'Hometown', 'Ethnic');
	
	function edit($id = null) {
		$this->UnionMemberDetail->id = $id;
		$this->set('id', $id);
		$this->set('list_ethnic', $this->Ethnic->find('list'));
		$this->set('list_hometown', $this->Hometown->find('list'));
		$this->set('list_religion', $this->Religion->find('list'));
		
		if (empty($this->data)) {
			$this->data = $this->UnionMemberDetail->read();
		} else {
			if ($this->UnionMemberDetail->save($this->data)) {
				$this->flash(__('Thao tác thành công.', true), array('controller'=>'union_members','action' => 'index'));
			}
			else{
				$this->flash(__('Thao tác thất bại. Vui lòng thử lại', true), array('controller'=>'union_members','action' => 'index'));
			}
		}
	}
	
	function editSub($id, $current_union_id) {
		$this->UnionMemberDetail->id = $id;
		$this->set('id', $id);
		$this->set('union_id', $current_union_id);
		$this->set('list_ethnic', $this->Ethnic->find('list'));
		$this->set('list_hometown', $this->Hometown->find('list'));
		$this->set('list_religion', $this->Religion->find('list'));
		
		if (empty($this->data)) {
			$this->data = $this->UnionMemberDetail->read();
		} else {
			if ($this->UnionMemberDetail->save($this->data)) {
				$this->flash(__('Thao tác thành công.', true), array('controller'=>'union_members','action' => 'subUms',$this->data['UnionMember']['current_union_id']));
			}
			else{
				$this->flash(__('Thao tác thất bại. Vui lòng thử lại', true), array('controller'=>'union_members','action' => 'subUms',$this->data['UnionMember']['current_union_id']));
			}
		}
	}

function editUmFee($id){
		$this->UnionMemberDetail->id = $id;
		$this->set('id', $id);
		$union_id = $this->UnionMemberDetail->query("select current_union_id from union_members where id = '$id';");
		$this->set('union_id', $union_id[0]["union_members"]["current_union_id"]);
		
		if (empty($this->data)) {
			$this->data = $this->UnionMemberDetail->read();
		} else {
			if ($this->UnionMemberDetail->save($this->data)) {
				$this->flash(__('Thao tác thành công.', true), array('controller'=>'union_members','action' => 'unionUmsFee',$this->data['UnionMemberDetail']['union_id']));
			}
			else{
				$this->flash(__('Thao tác thất bại. Vui lòng thử lại', true), array('controller'=>'union_members','action' => 'unionUmsFee',$this->data['UnionMemberDetail']['union_id']));
			}
		}
    }
    
	
function multiEditFee(){
	
		if($this->data['SelectedUMs'])
		{
			$listSelect = array();
			foreach($this->data['SelectedUMs'] as $key => $value) {
				if($value != '0') {
					array_push($listSelect, $value);
		 		}
			}
			$this->set('members', $this->UnionMember->find('all', array('conditions'=>array('UnionMember.id'=>$listSelect))));
			$union_id = $this->UnionMemberDetail->query("select current_union_id from union_members where id = '$listSelect[0]';");
			$this->set('union_id', $union_id[0]["union_members"]["current_union_id"]);
			$this->Session->write('listMove', $listSelect);
		}
		else{
			$date = $this->data[UnionMemberDetail][last_date_of_union_fee];
			
			if($this->Session->check('listMove')){
				foreach($this->Session->read('listMove') as $value){
					$this->UnionMemberDetail->query("update union_member_details set last_date_of_union_fee = '$date' where id = '$value';");
					}
				$this->Session->delete('listMove');
				$this->flash(__('Thao tác thành công.', true), array('controller'=>'UnionMembers','action' => 'unionUmsFee', $this->data['UnionMemberDetail']['union_id']));
			}
			else{
				$this->flash(__('Thao tác thất bại, vui lòng thử lại', true), array('controller'=>'UnionMembers','action' => 'unionUmsFee', $this->data['UnionMemberDetail']['union_id']));
			}
		}
	}
}
?>